/**
 * @FileName: ThemeController.java
 * @Author
 * @Description:
 * @Date 2016年1月4日 下午5:53:11
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.controller.h5;

import java.io.IOException;
import java.net.URLEncoder;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.kit.StrKit;

import cn.hpclub.server.constant.H5ApiConstant;
import cn.hpclub.server.model.CmccCity;
import cn.hpclub.server.model.MemberOrg;
import cn.hpclub.server.service.AsiaService;
import cn.hpclub.server.util.DESCrypto;
import cn.hpclub.server.util.SystemConfigUtil;

public class AsiaController extends BaseH5Controller implements H5ApiConstant{
    private static Logger log = LoggerFactory.getLogger(AsiaController.class);

    public void remoteAccess(){
        JSONObject jo = setResponseObj(H5ApiConstant.ResultCode.FAIL_CODE, H5ApiConstant.Msg.FAIL, null);
        String err_url = SystemConfigUtil.getSystemConfig().getHostPrefix() + getRequest().getContextPath()
                + "/website/zhishiku.html";
        // String err_url = "http://localhost/website/zhishiku.html";
        try{
            long userId = getParaToLong(H5ApiConstant.Ui.USERID_UPPER);
            String staffId = "";
            String empCode = "";
            String staff = "";
            MemberOrg memberInfo = AsiaService.service.getMemberOrgInfoByUserId(userId);
            if(memberInfo != null){
                staffId = memberInfo.getStr(CmccCity.LOGIN_NO);
                empCode = memberInfo.getStr(MemberOrg.EMP_CODE);
            }
            log.warn("staffId = " + staffId + ",,,,,empCode = " + empCode);

            if(StrKit.notBlank(staffId))
                staff = DESCrypto.encryptZSK(staffId);

            JSONObject temp = AsiaService.service.remoteAccess(staff, H5ApiConstant.ZHISHIKU_MODULEID);
            log.warn("request yaxin api result =" + temp);

            JSONObject result = new JSONObject();
            if(temp != null){
                if(temp.getInt(AsiaService.service.LOGIN_FLAG) != 0){// 0：表示验证通过，用户具有访问权限；1：表示验证未通过，用户无访问掌上微客权限；2：表示验证未通过，用户有访问掌上微客权限，但没有访问对应模块的权限。
                    jo = setResponseObj(H5ApiConstant.ResultCode.FAIL_CODE,
                                        temp.getString(AsiaService.service.ERROR_MSG), null);
                } else{
                    result.put(AsiaService.service.MODULE_URL,
                               temp.getString(AsiaService.service.MODULE_URL) + "?" + AsiaService.service.STAFF_ID + "="
                                       + staff + "&" + AsiaService.service.BOSS_ID + "=" + empCode + "&"
                                       + AsiaService.service.SESSION_ID + "="
                                       + temp.getString(AsiaService.service.SESSION_ID));

                    // jo =
                    // setResponseObj(H5ApiConstant.ResultCode.SUCCESS_CODE,
                    // H5ApiConstant.Msg.SUCCESS, result);

                    log.warn("redirect url = " + result.getString(AsiaService.service.MODULE_URL));

                    getResponse().sendRedirect(result.getString(AsiaService.service.MODULE_URL));

                    return;
                }
            }

        }
        catch(Exception e){
            jo = setResponseObj(H5ApiConstant.ResultCode.EXCEPTION_CODE, H5ApiConstant.Msg.EXCEPTION, null);
            e.printStackTrace();
        }

        try{
            log.warn("redirect url = " + err_url + "?errMsg=" + jo.get(H5ApiConstant.Key.MESSAGE));
            String encodeParam = URLEncoder.encode(jo.get(H5ApiConstant.Key.MESSAGE).toString());
            getResponse().sendRedirect(err_url + "?errMsg=" + encodeParam);

            // getResponse().getWriter().print(jo.get(H5ApiConstant.Key.MESSAGE));
        }
        catch(IOException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // renderText(jo.toString());
    }

}
